package com.um.jdy.mp.device.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.um.jdy.common.device.entity.vo.GunVO;
import com.um.jdy.common.device.entity.vo.PileVO;
import com.um.jdy.mp.device.entity.dto.DeliveryDTO;
import com.um.jdy.mp.device.entity.dto.DeviceDTO;
import com.um.jdy.mp.device.entity.dto.PilePageDTO;
import com.um.jdy.mp.device.entity.dto.RecycleDTO;
import com.um.jdy.mp.device.entity.vo.PileItemVO;
import com.um.jdy.mp.device.manager.DeviceManager;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.ListParams;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @author 甘天伟
 * @date 2022/1/25
 * 版权所有 广州优匠科技有限公司
 */
@Api(tags = "设备管理")
@Validated
@RestController
@RequestMapping("/api/v1/cms/device")
@SaCheckLogin
public class PileController extends BaseController<DeviceManager> {

    /**
     * 设备统计
     * @return
     */
    @ApiOperation(value = "设备统计")
    @PostMapping("center")
    public Response getCenter() {
        return manager.getCenter();
    }

    /**
     * 列表
     * @param name
     * @param device_type
     * @param station_id
     * @param status
     * @return
     */
    @ApiOperation("列表")
    @GetMapping({"list"})
    public Response<List<PileItemVO>> list(@ApiParam(value = "设备名称") String name,
                                           @ApiParam(value = "设备类型") String device_type,
                                           @ApiParam(value = "网点ID") String station_id,
                                           @ApiParam(value = "设备状态") String status) {
        ListParams params = new ListParams();
        if (!StringUtil.isEmpty(name)) {
            params.getCondition().put("p.name", StringUtil.getQueryParam(name));
        }
        if (!StringUtil.isEmpty(device_type)) {
            params.getCondition().put("p.device_type", device_type);
        }
        if (!StringUtil.isEmpty(status)) {
            params.getCondition().put("p.device_status", status);
        }
        if (!StringUtil.isEmpty(station_id)) {
            params.getCondition().put("p.device_station_id", station_id);
        }
        return manager.list(params);
    }

    /**
     * 分页列表
     * @param page_no
     * @param page_size
     * @param name
     * @param device_type
     * @param station_id
     * @param status
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("page")
    public Response<Page<PileItemVO>> page(@ApiParam(value = "页码") @NotNull Integer page_no,
                                           @ApiParam(value = "数据量") @NotNull Integer page_size,
                                           @ApiParam(value = "设备名称") String name,
                                           @ApiParam(value = "设备类型") String device_type,
                                           @ApiParam(value = "网点ID") String station_id,
                                           @ApiParam(value = "设备状态") String status) {

        PageParams params = new PageParams();
        params.setPageNo(page_no);
        params.setPageSize(page_size);
        if (!StringUtil.isEmpty(name)) {
            params.getCondition().put("p.name", StringUtil.getQueryParam(name));
        }
        if (!StringUtil.isEmpty(device_type)) {
            params.getCondition().put("p.device_type", device_type);
        }
        if (!StringUtil.isEmpty(status)) {
            params.getCondition().put("p.device_status", status);
        }
        if (!StringUtil.isEmpty(station_id)) {
            params.getCondition().put("p.device_station_id", station_id);
        }
        return manager.page(params);
    }

    /**
     * 端口列表
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "端口列表")
    @GetMapping("gun/{id}/list")
    public Response<List<GunVO>> getGunList(@PathVariable String id) {
        return manager.getGunList(id);
    }

    /**
     * 编辑名称
     * @param id
     * @param name
     * @return
     */
    @ApiOperation(value = "编辑名称")
    @PutMapping("{id}/edit_name")
    public Response updateName(@ApiParam(value = "设备ID") @PathVariable @NotNull String id,
                               @ApiParam(value = "设备名称") @NotNull String name) {
        return manager.updateName(id, name);
    }

    /**
     * 设备库存分页查询
     * @param params
     * @return
     */
    @ApiOperation(value = "设备库存分页查询")
    @PostMapping("/storage_page")
    public Response<Page<PileVO>> getPileStoragePage(@RequestBody PilePageDTO params){
        String deviceType = StringUtil.getTrim(params.getDeviceType().replaceAll("%",""),"%");
        params.setDeviceType(deviceType);
        params.setName("%" + params.getName().replaceAll("%","") + "%");
        PageParams pageParams = params.toPageParams(new String[]{"name","code","imei","deviceType"},new String[]{"d.name","d.code","d.imei","d.device_type"});
        return manager.getPileStoragePage(pageParams);
    }


    /**
     * 扫描二维码/输入编码
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "扫码, 运营中设备")
    @GetMapping("scan")
    public Response<PileVO> scanCode(@ApiParam(value = "设备编码") @RequestParam @NotNull String code) {
        return manager.scanCode(code);
    }

    @ApiOperation(value = "扫码，库存设备")
    @GetMapping("scan_inventory")
    public Response<PileVO> scanInventory(@ApiParam(value = "设备编码") @RequestParam @NotNull String code) {
        return manager.scanInventory(code);
    }

    @ApiOperation(value = "绑定")
    @PostMapping("bind")
    public Response bind(@RequestBody DeviceDTO deviceDTO) {
        manager.bind(deviceDTO);
        return Response.success();
    }

    /**
     * 设备回收
     *
     * @param recycleDTO
     * @return
     */
    @ApiOperation(value = "设备回收")
    @PostMapping("recycle")
    public Response recycle(@RequestBody RecycleDTO recycleDTO) {
        manager.recover(recycleDTO.getCode());
        return Response.success();
    }

    /**
     * 设备转移
     *
     * @param deviceDTO
     * @return
     */
    @ApiOperation(value = "设备转移")
    @PostMapping("transfer")
    public Response transfer(@RequestBody DeviceDTO deviceDTO) {
        manager.transfer(deviceDTO);
        return Response.success();
    }

    /**
     * 下发到运营商
     * @param deliveryDTO
     * @return
     */
    @ApiOperation(value = "设备下发到运营商")
    @PostMapping("arrange_to_merchant")
    public Response arrangeToMerchant(@RequestBody DeliveryDTO deliveryDTO) {
        manager.arrangeToMerchant(deliveryDTO);
        return Response.success();
    }
}
